package cn.lg.soar.common.util;

import java.io.File;
import java.net.FileNameMap;
import java.net.URLConnection;

/**
 * 文件类型工具
 * @author luguoxiang 469599794@qq.com
 * @Date: 2024/6/5 8:44
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public abstract class FileTypeUtils {

    private static final FileNameMap FILE_TYPE_MAP = URLConnection.getFileNameMap();

    public static String getContentType(File file) {
        return FILE_TYPE_MAP.getContentTypeFor(file.getName());
    }

    public static String getContentType(String filename) {
        return FILE_TYPE_MAP.getContentTypeFor(filename);
    }

    public static boolean isImage(File file) {
        return isImage(file.getName());
    }

    public static boolean isImage(String filename) {
        String contentType = getContentType(filename);
        if (contentType == null) {
            return false;
        }
        return contentType.startsWith("image/");
    }

    public static boolean isVideo(File file) {
        return isVideo(file.getName());
    }

    public static boolean isVideo(String filename) {
        String contentType = getContentType(filename);
        if (contentType == null) {
            return false;
        }
        return contentType.startsWith("video/");
    }

}
